#include<bits/stdc++.h>
using namespace std;

const int m[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};
int n, q, x, y;
int b[1005];

int main() {
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    
    sort(b, b + n);
    
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        int j;
        for (j = 0; j < n; j++) {
            if (b[j] % m[x] == y) {
                break;
            }
        }
        if (j < n) {
            cout << b[j] << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
    
    return 0;
}
